<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="96px"
    >
      <el-form-item label="文档合集名称" prop="noteName">
        <el-input
          v-model="queryParams.noteName"
          placeholder="请输入文档合集名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dmsIms:note:add']"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <div class="main-card-content">
      <el-card
        class="card-content"
        v-for="note in noteList"
        :key="note.id"
        shadow="hover"
        :body-style="note.noteStyle"
      >
        <div slot="header">
          <div>
            <span style="font-size: 20px; font-weight: bolder">
              {{ note.noteName }}
              <i
                style="float: right; line-height: 26px"
                v-show="note.noteType === 2"
                class="el-icon-lock"
              />
            </span>
          </div>
        </div>
        <div style="font-size: 14px; color: gray">
          {{ note.noteDescription }}
        </div>
        <div style="position: absolute; right: 10px; bottom: 5px">
          <el-link
            :underline="false"
            @click="handleDelete(note.id)"
            type="danger"
            icon="el-icon-delete"
            v-hasPermi="['dmsIms:note:del']"
          >
            删除
          </el-link>
          <el-divider direction="vertical"></el-divider>
          <el-link
            :underline="false"
            @click="handleUpdate(note.id)"
            type="success"
            icon="el-icon-edit"
            v-hasPermi="['dmsIms:note:edit']"
          >
            修改
          </el-link>
          <el-divider direction="vertical"></el-divider>
          <el-link
            :underline="false"
            type="primary"
            @click="noteFile(note.id)"
            icon="el-icon-right"
          >
            进入
          </el-link>
        </div>
      </el-card>
    </div>

    <!-- <el-table v-loading="loading" :data="noteList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="文档合集id" align="center" prop="id" />
      <el-table-column label="文档合集名称" align="center" prop="noteName" />
      <el-table-column label="文档合集类型" align="center" prop="noteType" />
      <el-table-column label="文档合集描述" align="center" prop="noteDescription" />
      <el-table-column label="文档合集样式(大小;颜色;背景图等)" align="center" prop="noteStyle" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dmsIms:note:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dmsIms:note:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table> -->

    <!-- <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->

    <!-- 添加或修改文档合集对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="96px">
        <el-form-item label="文档合集名称" prop="noteName">
          <el-input v-model="form.noteName" placeholder="请输入文档合集名称" />
        </el-form-item>
        <el-form-item label="文档合集类型" prop="noteType">
          <template>
            <el-radio v-model="form.noteType" :label="1">普通文档合集</el-radio>
            <el-radio v-model="form.noteType" :label="2">加密文档合集</el-radio>
          </template>
        </el-form-item>
        <el-form-item
          label="文档合集密码"
          prop="notePassword"
          v-show="form.noteType === 2"
        >
          <el-input
            v-model="form.notePassword"
            placeholder="请输入文档合集密码"
            :show-password="true"
          />
        </el-form-item>
        <el-form-item label="文档合集描述" prop="noteDescription">
          <el-input
            v-model="form.noteDescription"
            placeholder="请输入文档合集描述"
          />
        </el-form-item>
        <!-- <el-form-item label="文档合集样式" prop="noteStyle">
          <el-input
            v-model="form.noteStyle"
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 4 }"
            placeholder="请输入内容"
          />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-preventReClick
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listNote,
  getNote,
  delNote,
  addNote,
  updateNote,
} from "@/api/dmsIms/note";
import { encrypt, decrypt } from "@/utils/jsencrypt";
export default {
  name: "Note",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文档合集表格数据
      noteList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        noteName: null,
        noteType: null,
        noteDescription: null,
        noteStyle: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询文档合集列表 */
    getList() {
      this.loading = true;
      listNote(this.queryParams).then((response) => {
        this.noteList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        noteName: null,
        noteType: 1,
        notePassword: null,
        noteDescription: null,
        noteStyle: null,
        status: 0,
        createTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      let temp = encrypt("123");
      this.reset();
      this.open = true;
      this.title = "添加文档合集";
    },
    /** 修改按钮操作 */
    handleUpdate(noteId) {
      this.reset();
      const id = noteId;
      getNote(id).then((response) => {
        this.form = response.data;
        if (this.form.noteType === 2) {
          this.form.notePassword = decrypt(this.form.notePassword);
          this.judgePwd(this.form.notePassword, (temp) => {
            if (temp) {
              this.open = true;
              this.title = "修改文档合集";
            } else {
              this.$message.error("密码输入错误");
            }
          });
        } else {
          this.open = true;
          this.title = "修改文档合集";
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.noteType === 1) {
            this.form.notePassword = "";
          }
          if (
            this.form.notePassword !== null &&
            this.form.notePassword !== ""
          ) {
            this.form.notePassword = encrypt(this.form.notePassword);
          }

          if (this.form.id != null) {
            updateNote(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNote(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(noteId) {
      const ids = noteId;
      getNote(noteId)
        .then((response) => {
          this.form = response.data;
          this.form.notePassword = decrypt(this.form.notePassword);
        })
        .then((_) => {
          if (this.form.noteType === 2) {
            this.judgePwd(this.form.notePassword, (temp) => {
              if (temp) {
                this.$modal
                  .confirm("是否确认删除该文档合集")
                  .then(function () {
                    return delNote(ids);
                  })
                  .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                  })
                  .catch(() => {});
              } else {
                this.$message.error("密码输入错误");
              }
            });
          } else {
            this.$modal
              .confirm("是否确认删除该文档合集")
              .then(function () {
                return delNote(ids);
              })
              .then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
              })
              .catch(() => {});
          }
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "dmsIms/note/export",
        {
          ...this.queryParams,
        },
        `note_${new Date().getTime()}.xlsx`
      );
    },
    judgePwd(pwd, callback) {
      this.$prompt("请输入密码", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputType: "password",
      })
        .then(({ value }) => {
          if (value === pwd) {
            callback(true);
          } else {
            callback(false);
          }
        })
        .catch(() => {});
    },
    noteFile(noteId) {
      getNote(noteId)
        .then((response) => {
          this.form = response.data;
          this.form.notePassword = decrypt(this.form.notePassword);
        })
        .then((_) => {
          if (this.form.noteType === 2) {
            this.judgePwd(this.form.notePassword, (temp) => {
              if (temp) {
                this.$router.push({
                  path: "/file/document/index",
                  query: { noteId: noteId },
                });
              } else {
                this.$message.error("密码输入错误");
              }
            });
          } else {
            this.$router.push({
              path: "/file/document/index",
              query: { noteId: noteId },
            });
          }
        });
    },
  },
};
</script>

<style scoped>
.main-card-content {
  height: 70vh;
  display: flex;
  flex-wrap: wrap;
  overflow: auto;
}
.card-content {
  width: 300px;
  height: 200px;
  margin-left: 10px;
  margin-top: 10px;
  position: relative;
}
.card-content:hover {
}
.box {
  justify-content: space-around;
  flex-direction: column;
  justify-content: center;
  flex-grow: 1;
}
</style>
